package controller;

import bean.Area;
import com.google.gson.Gson;
import service.IAreaService;
import service.Impl.AreaServiceImpl;
import util.PageUtil;
import vo.AreaVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "AreaServlet",urlPatterns = "/area.do")
public class AreaServlet extends HttpServlet {
    IAreaService ias =new AreaServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equals("")){
            listByPage(request,response);
        }else if(method.equals("addArea")){
            addArea(request,response);
        }else if(method.equals("queryNoClassArea")){
            queryNoClassArea(request,response);
        }
    }
    protected void listByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String pageNoStr = request.getParameter("pageNo");
        String pageSizeStr =request.getParameter("pageSize");
        int pageNo =1;
        int pageSize =10;
        if(pageNoStr!=null){
            pageNo =Integer.parseInt(pageNoStr);
        }
        if(pageSizeStr!=null){
            pageSize =Integer.parseInt(pageSizeStr);
        }
//        f_areaName:$("#qaName").val(),
//        f_forestType:$("#qaWood").val(),
//        f_className:$("#qaClass").val()
        String f_areaName = request.getParameter("f_areaName");
        String f_forestType = request.getParameter("f_forestType");
        String f_className = request.getParameter("f_className");
        PageUtil<AreaVO> areaPageUtil =null;
        if(f_areaName==null&&f_forestType==null&&f_className==null){
            System.out.println("执行这个方法");
            areaPageUtil = ias.listByPage(pageNo, pageSize);

        }else{
            areaPageUtil = ias.listByPage(pageNo, pageSize,f_areaName,f_forestType,f_className);
        }


        Gson gson =new Gson();
        String json = gson.toJson(areaPageUtil);
        System.out.println("json = " + json);
        PrintWriter out = response.getWriter();
        out.write(json);
    }
    protected void addArea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String f_areaName = request.getParameter("f_areaName");
        String f_landTpye = request.getParameter("f_landType");
        String f_forestType = request.getParameter("f_forestType");
        String f_advantageTreeType = request.getParameter("f_advantageTreeType");
        Area a =new Area(f_areaName,f_forestType,f_landTpye,f_advantageTreeType);
        boolean b = ias.addOneArea(a);
//        System.out.println("a = " + a);
//        System.out.println("f_landTpye = " + f_landTpye);
        if(b){
//            PrintWriter out = response.getWriter();
//            out.write("true");
            response.sendRedirect("/pestSystem/home/areaData.html");
        }else{
            System.out.println("添加失败");
            System.out.println("a = " + a);
        }


    }
    protected void queryNoClassArea(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Area> noClassArea = ias.getNoClassArea();
        Gson gson =new Gson();
        String json = gson.toJson(noClassArea);
        PrintWriter out = response.getWriter();
        out.write(json);
    }
}
